package classDAO;

import java.util.List;

import mappedClass.Team;
import mappedClass.User;
import mappedClass.UserTeamHistory;

import org.hibernate.Query;
import org.hibernate.Session;

import testhibernate.HibernateUtil;

public class TeamDAO implements DAO {

	public List findAll() {
		Session s = HibernateUtil.getSessionFactory().openSession();
		List list = s.createQuery("from Team t").list();
		s.close();
		return list;
	}

	public Object getById(int id) {
		Session s = HibernateUtil.getSessionFactory().openSession();

		return (Team) s.get(Team.class, id);
	}

	public int getIdByName(String name) {
		Session s = HibernateUtil.getSessionFactory().openSession();

		Query q = s.createQuery("SELECT id FROM Team t WHERE t.name= :name")
				.setString("name", name);
		
		int id = (Integer)q.uniqueResult();
		return id;
	}
	
	public boolean userTeamHistory (String team, User user, Session s){

		int teamId = getIdByName(team);
		//UserTeamHistoryId newUTHId = new UserTeamHistoryId(teamId, user.getId());
		UserTeamHistory newUTH = new UserTeamHistory(((Team)getById(teamId)), user);
		s.save(newUTH);
		
		return true;
	}
	
	
}
